package com.canbot.u05.activity.base

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.bean.guide.LocalPointsBean
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.utils.OkHttpUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.free_jop_activity.*
import kotlinx.android.synthetic.main.free_tv_item.view.*
import java.util.HashMap
import kotlin.collections.ArrayList
import kotlin.collections.set

/**
 * Created by zmp on 2019/6/4 10:58
 * @author zmp
 * 空闲状态待机点选择界面
 */
class BaseChooseMoreJopActivity : BaseActivityNoTitle() {

    private lateinit var myAdapter: MyAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.free_jop_activity)
        initView()
        getData()
    }

    private fun getData() {
        showBaseLoadDialog()
        val url = IpMsgConst.HTTP_URL_CHEST + "guideMapAction"
        val hashMap = HashMap<String, Any>()
        hashMap["type"] = 8
        hashMap["data"] = 1
        OkHttpUtils.upLoadFile(url, hashMap, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                dismissBaseDialog()
                Log.e("TTS2Voice", result)
                if (TextUtils.isEmpty(result)) {
                    showBaseHintDialog(getString(R.string.no_map_data), true)
                    return
                }
                val gSonType = object : TypeToken<java.util.ArrayList<LocalPointsBean>>() {
                }.type
                val siteList = Gson().fromJson<ArrayList<LocalPointsBean>>(result, gSonType)
                jopArr.clear()
                jopArr.addAll(siteList)
                runOnUiThread {
                    myAdapter.notifyDataSetChanged()
                }
            }

            override fun onFailure() {
                dismissBaseDialog()
                showBaseHintDialog(getString(R.string.service_error_hint), true)
            }
        })
    }


    var jopArr = ArrayList<LocalPointsBean>()
    var getArr = ArrayList<String>()

    private fun initView() {
        back_btn_head.setOnClickListener {
            finish()
        }

        saveTextView.setOnClickListener {
            if (getArr.isNotEmpty()) {
                setResult(Activity.RESULT_OK, Intent()
                        .putStringArrayListExtra("points", getArr)
                        .putExtra("mapID", jopArr[0].mapId))
                finish()
            } else {
                showBaseHintDialog(getString(R.string.pleas_choose_area))
            }
        }

        jopRecyclerView.layoutManager = GridLayoutManager(this, 3)
        myAdapter = MyAdapter(this, jopArr)
        jopRecyclerView.adapter = myAdapter

    }


    internal inner class MyAdapter(context: Context, var arr: ArrayList<LocalPointsBean>) : RecyclerView.Adapter<MyHolder>() {

        private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
            val inflate = layoutInflater.inflate(R.layout.free_tv_item, parent, false)
            return MyHolder(inflate)
        }

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            if (getItemViewType(position) == 1) {
                return
            }
            val str = arr[position]
            holder.updateView(str, position)
        }

        override fun getItemCount(): Int {
            return arr.size
        }

    }

    internal inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        var textView: TextView = itemView.textView

        init {
            textView.setOnClickListener(this)
        }

        override fun onClick(v: View) {
            val pointName = jopArr[adapterPosition].pointName
            if (getArr.contains(pointName)) {
                getArr.remove(pointName)
            }else{
                getArr.add(pointName)
            }
            myAdapter.notifyDataSetChanged()
        }

        fun updateView(str: LocalPointsBean, position: Int) {
            textView.text = str.pointName
            textView.isSelected = getArr.contains(str.pointName)
        }
    }
}